const path = require("path");
const fs = require("fs");
module.exports = {
  isTokenValid: (token, client) => {
    return new Promise((resove, reject) => {
      client.hgetall(token, (err, value) => {
        if (err) {
          resove(false);
        } else {
          if (value) {
            let { creatTime, expires } = value;
            let now = Date.now();
            if (now - creatTime > expires) {
              resove(false);
            } else {
              resove(true);
            }
          } else {
            resove(false);
          }
        }
      });
    });
  },
  mapFiles(folderName) {
    let exportFiles = {};
    let modelUrl = path.resolve(__dirname, folderName);
    let files = fs.readdirSync(modelUrl);
    let js_files = files.filter((f) => {
      return f.endsWith(".js");
    }, files);

    for (let f of js_files) {
      let name = f.substring(0, f.length - 3);
      exportFiles[name] = require(path.resolve(modelUrl, f));
    }

    return exportFiles;
  },
};
